$(function(){
	//更新行数据
	function authRuleUpdateActions(value){
		$.post(authRuleEditUrl,{
				id:value.id,
				o:value.o,
				icon:value.icon,
				is_menu:value.is_menu,
				name:value.name,
				contr:value.contr,
				func:value.func,
				condition:value.condition,
				edit:1
				},function(result){
            if (!result.status){
                $.messager.alert('错误提示',result.message,'error');
            }
        },'json').error(function(data){
        	var info=eval('('+data.responseText+')');
			$.messager.confirm('错误提示',info.message,function(r){
				//$('#authAccessSetDialog').dialog('close');
			});
        });
	};

	var editIndex = undefined;
	function endEditing(){
		if (editIndex == undefined){return true}
		if ($('#authRuleDatagrid').datagrid('validateRow', editIndex)){
			$('#authRuleDatagrid').datagrid('endEdit', editIndex);
			editIndex = undefined;
			return true;
		} else {
			return false;
		}
	};
	//双击行编辑
	function onDblClickRow(index){
		if (editIndex != index){
			if (endEditing()){
				$('#authRuleDatagrid').datagrid('selectRow', index);
				$('#authRuleDatagrid').datagrid('endEdit', editIndex);
				$('#authRuleDatagrid').datagrid('beginEdit', index);
				editIndex = index;
			} else {
				$('#authRuleDatagrid').datagrid('selectRow', editIndex);
			}
		}
	};
	$("#authRuleDatagrid").datagrid({
		title:'规则列表',
		url:authRuleUrl,
		fitColumns:true,
		rownumbers:true,
		pageSize:300,
		pageNumber:1,
		pageList:[10,15,20,25,30,40,50,100,300],
		columns:[[
		{field:'id',title:'规则id',checkbox:true},
		{field:'name',title:'规则标识',
				editor: {
					type: 'textbox',
				}
		},
		{field:'title',title:'规则简述'},
		{field:'contr',title:'控制器',
				editor: {
					type: 'textbox',
				}
		},
		{field:'func',title:'方法',
				editor: {
					type: 'textbox',
				}
		},
		{field:'condition',title:'附加条件',
				editor: {
					type: 'textbox',
				}
		},
		{field:'moduleName',title:'所属模块',width:'200px'},
		{field:'status',title:'状态',formatter:checkYesNo,width:'70px'},
		{field:'o',title:'排序',
				editor: {
					type: 'numberbox',  
					options: { 
						required: true,  
						missingMessage: '不能为空',
						min:0
					}  
				}
		},
		{field:'icon',title:'Icon图标',
				editor: {
					type: 'textbox'
				}
		},
		{field:'is_menu',title:'显示菜单',
				formatter:function(val,rowData,rowIndex){
			        if(val==1){
			            return '是';
					}
					if(val==0){
			            return '否';
					}
			    },
				editor:{
					type:'combobox',
					options:{
						valueField:'label',
						textField:'value',
						data: [{
							label: 1,
							value: '是'
						},{
							label: 0,
							value: '否'
						}],
						required:true,
						validType:['selected[]']
					}
				}
		}
		]],
		onDblClickRow: onDblClickRow,
		onAfterEdit:function(index,row){
			//console.log(row);
					row.editing = false;
					authRuleUpdateActions(row);
		},
		pagination:true,
		fit:true,
	});
});

authRuleObj={
	add:function(url){
		$('#authRuleAddForm').form('submit',{
			url:url,
            onSubmit: function(){
                return $(this).form('validate');
            },
            success: function(result){
            	var result=eval('('+result+')');
                if (!result.status){
                    $.messager.confirm('错误提示',result.message,function(r){
                    	$('#authRuleAddDialog').dialog('close');  
                    });
                } else {
                    $('#authRuleAddDialog').dialog('close');       
                    $('#authRuleDatagrid').datagrid('reload'); 
                }
            }

		});
	},
	save:function(url){
		$('#authRuleEditForm').form('submit',{
            url: url,
            onSubmit: function(){
                return $(this).form('validate');
            },
            success: function(result){
            	 var result=eval('('+result+')');
                if (!result.status){
                    $.messager.confirm('错误提示',result.message,function(r){
                    	$('#authRuleEditDialog').dialog('close');  
                    });
                }else{
                    $('#authRuleEditDialog').dialog('close');      
                    $('#authRuleDatagrid').datagrid('reload');    
                }
            }
        });
	},
	showAll:function(){
		$('#authRuleDatagrid').datagrid({ queryParams:''});
	},
	addBar:function(){
		$('#authRuleAddDialog').dialog('open');
		$('#authRuleAddForm').form('clear');
		$('#authRuleStatus').combobox('select',1);
		$('#authRuleMid').combobox('select',0);
		$('#authRuleismenu').combobox('select',1);
		
	},
	editBar:function(){
		var authRuleRow =$('#authRuleDatagrid').datagrid('getSelections');
        if (authRuleRow.length>1){		            	
            $.messager.alert('提示','一次只能修改一条记录!','info');
        }else if(authRuleRow.length==1){
			str_lastindex=authRuleRow[0].title.lastIndexOf("|");//取得最后一个匹配"|"的下标
			str_title=authRuleRow[0].title.substr(str_lastindex+1);
        	$('#authRuleEditDialog').dialog('open');
            $('#authRuleEditForm').form('load',authRuleRow[0]);
			$('#rule_edit_title').textbox('setValue',str_title);
			
        }else{
			$.messager.alert('推送提示','请选择列表数据','error');
		}
	},
	removeBar:function(){
		var authRuleRow =$('#authRuleDatagrid').datagrid('getSelections');
        if (authRuleRow.length>0){
        	$.messager.confirm('删除提示','真的要删除'+authRuleRow.length+'条信息吗?删除将不能再恢复！',function(r){
                if (r){
                	var ids=[];
                	for(var i=0;i<authRuleRow.length;i++){
                		ids.push(authRuleRow[i].id);
                	}
                	var id=ids.join(',');
                    $.post(authRuleDelUrl,{id:id},function(result){
                        if (result.status){
                            $('#authRuleDatagrid').datagrid('reload');
                        }else{
                            $.messager.alert('错误提示',result.message,'error');
                        }
                    },'json').error(function(data){
                    	var info=eval('('+data.responseText+')');
						$.messager.confirm('错误提示',info.message,function(r){
							
						});
                    });
                }
        	});
		}else{
			$.messager.alert('推送提示','请选择列表数据','error');
		}
	},
	reloadBar:function(){//刷新数据
		$('#authRuleDatagrid').datagrid('reload');
	},
	saveBar:function(){//保存数据
		var editIndex = undefined;
		if (editIndex == undefined){endEditing = 1;}
		if ($('#authRuleDatagrid').datagrid('validateRow', editIndex)){
			$('#authRuleDatagrid').datagrid('endEdit', editIndex);
			editIndex = undefined;
			endEditing = 1;
		} else {
			endEditing = 0;
		}
		if (endEditing == 1){
			$('#authRuleDatagrid').datagrid('acceptChanges');
		}
	}
}